package com.gator.desig_mode.prototype;

import org.springframework.beans.factory.support.AbstractBeanFactory;

/**
 * @author 何章怀晓 hezhanghuaixiao
 * @version V1.0
 * @desc
 */
public class A {

  public static void main(String[] args) throws CloneNotSupportedException {

    C c = new C(new B(), "曹操");
    C clone = (C) c.clone();
    System.out.println(c.getB().hashCode() + "-----------" + c.toString());
    System.out.println(clone.getB().hashCode() + "--------------" + clone.toString());



  }

}


class C implements Cloneable {

  private B b;
  private String name;

  public C(B b, String name) {
    this.b = b;
    this.name = name;
  }

  @Override
  public String toString() {
    return "C{" +
        "b=" + b +
        ", name='" + name + '\'' +
        '}';
  }

  public B getB() {
    return b;
  }

  public void setB(B b) {
    this.b = b;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  @Override
  protected Object clone() throws CloneNotSupportedException {
    Object returnValve = null;
    returnValve = super.clone();
    //引用处理
    C c = (C) returnValve;
    c.setB(b.clone());
    return c;
  }
}

class B implements Cloneable {

  int a = 10;

  B b;

  public B(int a, B b) {
    this.a = a;
    this.b = b;
  }

  public B() {

  }

  public int getA() {
    return a;
  }

  public void setA(int a) {
    this.a = a;
  }

  public B getB() {
    return b;
  }

  public void setB(B b) {
    this.b = b;
  }

  @Override
  protected B clone() throws CloneNotSupportedException {
    B b = new B();
    b = (B) super.clone();

    return b;
  }

  @Override
  public String toString() {
    return "B{" +
        "a=" + a +
        ", b=" + b +
        '}';
  }
}
